<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="_header_include::frag(~{::title},~{},~{})">
    <title>后台管理系统</title>
    <style>
    </style>
</head>
<body>
<div id="maindiv" class="maindiv mainPadding" style="display: none;">    
    <el-breadcrumb separator="/" class="elBreadcrumb marginB20">
        <el-breadcrumb-item>Home</el-breadcrumb-item>
        <el-breadcrumb-item>数据报表</el-breadcrumb-item>
        <el-breadcrumb-item>资源分配处理明细表</el-breadcrumb-item>
    </el-breadcrumb>    
    <el-row class="marginB20">
        <el-button type="primary" @click="searchYesterday">昨天</el-button>
        <el-button type="success" @click="searchWeek">近七天</el-button>
        <el-button type="warning" @click="searchMonth">本月</el-button> 
         <shiro:hasPermission name="statistics:trafficCallResource:export">
        <el-button type="danger" @click="exportRs">导出Excel报表</el-button>   
         </shiro:hasPermission>
    </el-row>
    <div class="mainSearchBg">        
        <div class="mainSearchFormBox">
           <el-form :inline="true" :model="form" class="demo-form-inline mainSearchForm" ref="form">                 
                <el-form-item label="统计日期:">
                    <el-date-picker format="yyyy-MM-dd HH:mm" v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="—" class="widthauto">
                   <el-date-picker format="yyyy-MM-dd HH:mm" v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="资源类别:">
                    <el-select v-model="form.category" placeholder="资源类别" clearable filterable>
                        <el-option
                            v-for="item in clueCategoryList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否新资源:">  
                    <el-select v-model="form.newResource" placeholder="全部" style="width: 100%;" clearable filterable>
                        <el-option label="是" value="1"></el-option>
                        <el-option label="否" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <div class="mainSearchBtnBox">
                    <el-button icon="el-icon-search" type="primary" @click="searchTable(1)" class="searchBtn" :disabled="loading">搜索</el-button>
                </div>  
            </el-form>
        </div>
        <el-row>
            <el-table
                style="width: 100%"
                border
                :data="tableData"
                v-loading="loading"
                :span-method="arraySpanMethod">
                <el-table-column type="index" label="序号" align="center" width="55" :index="indexFun"></el-table-column>
                <el-table-column
                    prop="newResource"
                    align="center"
                    label="是否新资源">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" v-if="scope.row.newResource == 99999" @click="gotoPerson()">合计</el-button>
                        <span type="text" size="small" v-else>{{scope.row.newResource?'是':'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="receiveCount" align="center" label="接收资源" width="80">
                </el-table-column>
                <el-table-column prop="assignCount" align="center" label="分配资源量" width="100">
                </el-table-column>
                <el-table-column prop="trackingCount" align="center" label="跟访量" width="80">
                </el-table-column>
                <el-table-column prop="transCount" align="center" label="转电销量" width="80">
                </el-table-column>
                <el-table-column prop="effectiveCount" align="center" label="有效资源" width="80">
                </el-table-column>
                <el-table-column prop="followRate" align="center" label="跟访率" :formatter="percentFormat" width="80">
                </el-table-column>
                <el-table-column prop="transRate" align="center" label="转电销率" :formatter="percentFormat" width="80">
                </el-table-column>
                <el-table-column prop="effectiveRate" align="center" label="资源有效率" :formatter="percentFormat" width="100">
                </el-table-column>
            </el-table>
            <table-pagination :pager="pager" @change="searchTable()"></table-pagination>
        </el-row>
    </div>
</div>
<!-- import common js-->
<div th:include="_footer_include :: #jsLib"></div>
</body>
<style>
.el-table tr:nth-of-type(1) td:nth-of-type(n+1) {
    font-weight: bold;
}
</style>
<script th:inline="javascript">
    var clueCategoryList=[[${clueCategoryList}]]; // 资源类别
    var mainDivVM = new Vue({
        el: '#maindiv',
        data: function() {
            return {
                loading: false,
                pager:{ 
                    total: 0,
                    currentPage: 1,
                    pageSize: 20
                },
                form: {
                    startTime: '',
                    endTime: '',
                    category: '',
                    newResource: ''
                },
                export: {
                    startTime: '',
                    endTime: '',
                    category: '',
                    newResource: ''
                },
                clueCategoryList: clueCategoryList,
                tableData: [
                    // {
                    //     newResource: 99999,
                    //     receiveCount: 0,
                    //     assignCount: 0,
                    //     trackingCount: 0,
                    //     transCount: 0,
                    //     effectiveCount: 0,
                    //     followRate : 0,
                    //     transRate: 0,
                    //     effectiveRate: 0,
                    //     days: '2019-01-01',
                    //     userName: '张三',
                    // },
                    // {
                    //     newResource: 1,
                    //     receiveCount: 0,
                    //     assignCount: 0,
                    //     trackingCount: 0,
                    //     transCount: 0,
                    //     effectiveCount: 0,
                    //     followRate : 0,
                    //     transRate: 0,
                    //     effectiveRate: 0,
                    //     days: '2019-01-01',
                    //     userName: '张三',
                    // },
                    // {
                    //     newResource: 0,
                    //     receiveCount: 0,
                    //     assignCount: 0,
                    //     trackingCount: 0,
                    //     transCount: 0,
                    //     effectiveCount: 0,
                    //     followRate : 0,
                    //     transRate: 0,
                    //     effectiveRate: 0,
                    //     days: '2019-01-01',
                    //     userName: '李四',
                    // },
                ]
            }             
        },
        methods: {
            searchTable(firstPage) { 
                var startTime = this.form.startTime;
                var endTime = this.form.endTime;
                if(endTime && startTime){
                    var startT = new Date(this.form.startTime);
                    var endT = new Date(this.form.endTime);
                    if(startT>endT){
                        this.$message({
                            message: '开始时间不能大于结束时间',
                            type: 'warning'
                        });
                        return;
                    }
                    var date = new Date();
                    var currentDate = moment(date).format("YYYY-MM-DD 23:59");
                    date.setMonth(date.getMonth()-6);
                    var beforeSixMonth = moment(date).format("YYYY-MM-DD 00:00");
                    var currentDateRes = new Date(currentDate);
                    var beforeSixMonthRes = new Date(beforeSixMonth);
                    if(startT < beforeSixMonthRes || endT > currentDateRes){
                        this.$message({
                            message: '请选择近6个月内的时间',
                            type: 'warning'
                        });
                        return;
                    }
                    startTime = this.limitTime(startTime, null);
                    endTime = this.limitTime(null, endTime);
                }else{
                    this.$message({
                        message: '请选择起止时间',
                        type: 'warning'
                    });
                    return;
                }
                var param = {};
                param.startTime = startTime;
                param.endTime = endTime;
                param.pageSize = this.pager.pageSize;
                if(firstPage){
                    param.pageNum = firstPage;
                }else{
                    param.pageNum = this.pager.currentPage;
                }
                param.category = this.form.category;
                param.newResource = this.form.newResource;
                this.loading = true; 
                axios.post('/statistics/trafficCallResource/getGroupPageList',param).then(function (response) {
                    if(!response || !response.data){
                        mainDivVM.$message({
                            message: "接口调用失败",
                            type: 'error'
                        });
                        return ;
                    }
                    var resobj= response.data;
                    if(resobj.code != '0'){
                        mainDivVM.loading = false;
                        mainDivVM.$message({
                            message: resobj.msg,
                            type: 'error'
                        });
                        return ;
                    }else if(resobj.data && resobj.data.tableData){
                        var resobjData = resobj.data;    
                        var tableData = resobjData.tableData;
                        mainDivVM.tableData = tableData.data;
                        if( tableData.data.length && resobj.data.totalData ){
                            var totalData = resobjData.totalData[0];
                            mainDivVM.tableData.unshift(totalData);
                        }
                        mainDivVM.pager.currentPage = tableData.currentPage;
                        mainDivVM.pager.total = tableData.total;
                        mainDivVM.loading = false;
                        mainDivVM.export.startTime = startTime;
                        mainDivVM.export.endTime = endTime;
                        mainDivVM.export.category = mainDivVM.form.category;
                        mainDivVM.export.newResource = mainDivVM.form.newResource;
 
                    }else{
                        mainDivVM.loading = false;
                    }
                }).catch(function (error) {
                    mainDivVM.loading = false;
                    mainDivVM.$message({
                        message: "请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            limitTime (start, end) {
                var startTime = this.minuteFormat (start);
                var endTime = this.minuteFormat (end);
                var date = new Date();
                data = this.minuteFormat (date);// 现在的时间
                data = data.slice(0,8);
                if(startTime){
                    var s = startTime.slice(0,8);
                    if(s < data){
                        this.form.startTime = moment(start).format("YYYY-MM-DD 00:00");
                        return this.form.startTime;
                    }
                    return start;
                }
                if(endTime){
                    var e = endTime.slice(0,8);
                    if(e < data){
                        this.form.endTime = moment(end).format("YYYY-MM-DD 23:59");
                        return this.form.endTime;                        
                    }
                    return end;
                }
            },
            arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合并第一行
                if (rowIndex === 0) {
                    if (columnIndex === 0) {
                        return [0, 0];
                    } else if (columnIndex === 1) {
                        return [1, 2];
                    }
                }
            },
            indexFun (index) { // 表格序号
                return index;
            },
            nullToZero(row, column, value, index){
            	if(value){return value;}
            	return 0;
            },
            gotoPerson (row) { // 页面跳转
                var category = this.export.category;
                var newResource = this.export.newResource;
                var startTime = this.export.startTime;
                var endTime = this.export.endTime;
                window.location.href="/statistics/trafficCallResource/resourceAllocationDisposePerson?category=" + category + "&newResource="+ newResource +"&startTime=" + startTime + "&endTime=" + endTime;
            },
            percentFormat (row, column) { // 百分比格式化fn
                var percent = row[column.property];
                if(!percent){ // 如果没有值或值为0
                    return 0 + "%";
                }
                percent = (percent * 1000)/10 + "%";
                return percent;
            },
            minuteFormat (m) { // 时间格式化201905050505fn
                if (!m) {return "";}
                var timeStr = new Date(m);
                var yearRes = timeStr.getFullYear();
                var monthRea = timeStr.getMonth()+1;
                monthRea = monthRea < 10 ?  "0" + monthRea : monthRea;
                var dateRes = timeStr.getDate();
                dateRes = dateRes < 10 ?  "0" + dateRes : dateRes;  
                var hoursRes = timeStr.getHours();
                hoursRes = hoursRes < 10 ?  "0" + hoursRes : hoursRes;  
                var minRes = timeStr.getMinutes();
                minRes = minRes < 10 ?  "0" + minRes : minRes;                
                var timeRes = "" + yearRes + monthRea + dateRes + hoursRes + minRes;
                return timeRes;
            },
            searchYesterday(){ // 昨天
               var today = new Date();
               today.setTime(today.getTime()-24*60*60*1000);
               var startTime = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 00:00";
               var endTime =today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 23:59";
               this.form.startTime=startTime;
               this.form.endTime=endTime;
               this.searchTable(1);
            },
            searchWeek(){ // 近七天
                 var a = new Date();
                 var year = a.getFullYear();
                 var month = a.getMonth();
                 var date = a.getDate();
                 var b = new Date(year,month,date);
                 var c = b.valueOf()-6*24*60*60*1000;
                 var d = b.valueOf();
                 var startTime= new Date(c);
                 var endTime = new Date(d);
                 this.form.startTime=startTime.getFullYear()+"-" + (startTime.getMonth()+1) + "-" + startTime.getDate()+" 00:00";
                 this.form.endTime=endTime.getFullYear()+"-" + (endTime.getMonth()+1) + "-" + endTime.getDate()+" 23:59";
                 this.searchTable(1);
            },
            searchMonth(){ // 本月 
                var a = new Date();
                var year = a.getFullYear();
                var month = a.getMonth();
                var date = a.getDate();
                var b = new Date(year,month,date);
                var f= new Date(year,month,01);
                var c = f.valueOf();
                var startTime= new Date(c);
                this.form.startTime=startTime.getFullYear()+"-" + (startTime.getMonth()+1) + "-" + startTime.getDate()+" 00:00";
                this.form.endTime=year+"-"+(month+1)+"-"+date+" 23:59";
                this.searchTable(1);
            },
            exportRs(){
                var param = {};
                param.startTime = this.export.startTime;
                param.endTime = this.export.endTime;
                param.category =  this.export.category; 
                param.newResource =  this.export.newResource; 
                axios.post('/statistics/trafficCallResource/exportGroupAllList',param,{responseType:'blob'})
                    .then(function (response) {
                        var data =  response.data;
                        var fileName = response.headers.filename;
                        saveAs(data,decodeURI(fileName));
                    })
                    .catch(function (error) {
                        mainDivVM.$message({
                            message: "导出失败，请稍后重试",
                            type: "error"
                        });
                    });
            }
        },
        created(){
            // 默认进入显示当天时间
            var today = new Date();
            var startTime = moment(today).format("YYYY-MM-DD 00:00");
            var endTime = moment(today).format("YYYY-MM-DD 23:59");
            this.form.startTime = startTime;
            this.form.endTime = endTime;
            this.export.startTime = startTime;
            this.export.endTime = endTime;

            var localVal=localStorage.getItem('allChangePageSize')?parseInt(localStorage.getItem('allChangePageSize')):'';
            if(localVal){this.pager.pageSize = localVal;}

            this.searchTable();
        },
        mounted(){
            document.getElementById('maindiv').style.display = 'block';
        }
    })
    
</script>
</html>